# Uncomment and run this cell to install numpy
# !pip install numpy
Numpy
- Numpy is a python package used for scientific computing
- Numpy provides arrays which are greater and faster alternatives to traditional python lists. An array is a group of elements of the same data type
- A standard numpy array is required to have elements of the same data type.
Inspecting our arrays
# import numpy module
import numpy as np
# checking the numpy version
np.__version__
'1.24.1'
# creating a numpy array
= np.array([1, 2, 3, 4]) num_arr
The object that’s created by array()
is called ndarray
. This can be shown by checking the type of the object using type()
# Checking type of object
type(num_arr)
numpy.ndarray
Data Types
The table below describes some of the most common data types we use in numpy
Data Type | Description |
---|---|
np.int64 |
Signed 64-bit integer types |
np.float32 |
Standard double-precision floating point |
np.complex |
Complex numbers represented by 128 floats |
np.bool |
Boolean type storing True and False values |
np.object |
Python object type |
np.string_ |
Fixed-length string type |
np.unicode_ |
Fixed-length unicode type |
# shape of array
num_arr.shape
(4,)
# finding the number of dimensions
num_arr.ndim
1
# number of elements in array
len(num_arr)
4
# another way to get the number of elements
num_arr.size
4
# finding data type of array elements
num_arr.dtype.name
'int64'
# converting an array
= np.array([1.2, 3.5, 7.0])
float_arr
# use astype() to convert to a specific
= float_arr.astype(int)
int_arr
print(f'Array: {float_arr}, Data Type: {float_arr.dtype}')
print(f'Array: {int_arr}, Data Type: {int_arr.dtype}')
Array: [1.2 3.5 7. ], Data Type: float64
Array: [1 3 7], Data Type: int64
Ask for help
np.info(np.ndarray.shape)
Tuple of array dimensions.
The shape property is usually used to get the current shape of an array,
but may also be used to reshape the array in-place by assigning a tuple of
array dimensions to it. As with `numpy.reshape`, one of the new shape
dimensions can be -1, in which case its value is inferred from the size of
the array and the remaining dimensions. Reshaping an array in-place will
fail if a copy is required.
.. warning::
Setting ``arr.shape`` is discouraged and may be deprecated in the
future. Using `ndarray.reshape` is the preferred approach.
Examples
--------
>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> y.shape = (3, 6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged
>>> np.zeros((4,2))[::2].shape = (-1,)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: Incompatible shape for in-place modification. Use
`.reshape()` to make a copy with the desired shape.
See Also
--------
numpy.shape : Equivalent getter function.
numpy.reshape : Function similar to setting ``shape``.
ndarray.reshape : Method similar to setting ``shape``.
?np.ndarray.shape
Type: getset_descriptor String form: <attribute 'shape' of 'numpy.ndarray' objects> Docstring: Tuple of array dimensions. The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with `numpy.reshape`, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions. Reshaping an array in-place will fail if a copy is required. .. warning:: Setting ``arr.shape`` is discouraged and may be deprecated in the future. Using `ndarray.reshape` is the preferred approach. Examples -------- >>> x = np.array([1, 2, 3, 4]) >>> x.shape (4,) >>> y = np.zeros((2, 3, 4)) >>> y.shape (2, 3, 4) >>> y.shape = (3, 8) >>> y array([[ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.]]) >>> y.shape = (3, 6) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: total size of new array must be unchanged >>> np.zeros((4,2))[::2].shape = (-1,) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Incompatible shape for in-place modification. Use `.reshape()` to make a copy with the desired shape. See Also -------- numpy.shape : Equivalent getter function. numpy.reshape : Function similar to setting ``shape``. ndarray.reshape : Method similar to setting ``shape``.
Array mathematics
Arithmetic Operations
# creating arrays
= np.array([1, 4, 6, 7])
array1 = np.array([3, 5, 3, 1]) array2
# subtract
= array1 - array2
difference1 print('difference1 =', difference1)
# another way
= np.subtract(array1, array2)
difference2 print('difference2 =', difference2)
difference1 = [-2 -1 3 6]
difference2 = [-2 -1 3 6]
# sum
= array1 + array2
summation1 print('summation1 =', summation1)
# another way
= np.add(array1, array2)
summation2 print('summation2 =', summation2)
summation1 = [4 9 9 8]
summation2 = [4 9 9 8]
Trigonometric operations
# sin
print('sin(array1) =', np.sin(array1))
# cos
print('cos(array1) =', np.cos(array1))
# log
print('log(array1) =', np.log(array1))
sin(array1) = [ 0.84147098 -0.7568025 -0.2794155 0.6569866 ]
cos(array1) = [ 0.54030231 -0.65364362 0.96017029 0.75390225]
log(array1) = [0. 1.38629436 1.79175947 1.94591015]
# dot product
array1.dot(array2)
48
Research:
- another way to dot matrices (arrays)
Comparison
== array2 array1
array([False, False, False, False])
> 3 array1
array([False, True, True, True])
Aggregate functions
# average
= array1.mean()
mean print('Mean: ', mean)
# min
= array1.min()
minimum print('Minimum: ', minimum)
# max
= array1.max()
maximum print('Maximum: ', maximum)
# corrcoef
= np.corrcoef(array1, array2)
correlation_coefficient print('Correlation Coefficient: ', correlation_coefficient)
= np.std(array1)
standard_deviation print('Standard Deviation: ', standard_deviation)
Mean: 4.5
Minimum: 1
Maximum: 7
Correlation Coefficient: [[ 1. -0.46291005]
[-0.46291005 1. ]]
Standard Deviation: 2.29128784747792
Research:
- copying arrays (you might meet
view()
,copy()
)
Subsetting, Slicing and Indexing
- Indexing is the technique we use to access individual elements in an array. 0 represents the first element, 1 the represents second element and so on.
- Slicing is used to access elements of an array using a range of two indexes. The first index is the start of the range while the second index is the end of the range. The indexes are separated by a colon ie
[start:end]
# Creating numpy arrays of different dimension
= np.array([1, 4, 6, 7]) # 1D array
arr1 print('Array1 (1D): \n', arr1)
= np.array([[1.5, 2, 3], [4, 5, 6]]) # 2D array
arr2 print('Array2 (2D): \n', arr2)
= np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
arr3 10, 11, 12], [13, 14, 15], [16, 17, 18]]]) #3D array
[[print('Array3 (3D): \n', arr3)
Array1 (1D):
[1 4 6 7]
Array2 (2D):
[[1.5 2. 3. ]
[4. 5. 6. ]]
Array3 (3D):
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]
[[10 11 12]
[13 14 15]
[16 17 18]]]
# find the dimensions of an array
arr3.shape
(2, 3, 3)
Indexing
# accessing items in a 1D array
2] arr1[
6
# accessing items in 2D array
0, 2] arr2[
3.0
# accessing in a 3D array
0, 2, 1] arr3[
8
slicing
# slicing 1D array
0:3] arr1[
array([1, 4, 6])
# slicing a 2D array
1, 0:2] arr2[
array([4., 5.])
# slicing a 3D array
= arr3[0, 2]
first = arr3[1, 0]
second
np.concatenate((first, second))
array([ 7, 8, 9, 10, 11, 12])
# boolean indexing
< 5] arr1[arr1
array([1, 4])
Research:
- Fancy Indexing
Array manipulation
print(arr2)
[[1.5 2. 3. ]
[4. 5. 6. ]]
# transpose
= np.transpose(arr2)
arr2_transpose1 print('Transpose1: \n', arr2_transpose1)
# another way
= arr2.T
arr2_transpose2 print('Transpose2: \n', arr2_transpose2)
Transpose1:
[[1.5 4. ]
[2. 5. ]
[3. 6. ]]
Transpose2:
[[1.5 4. ]
[2. 5. ]
[3. 6. ]]
# combining arrays
= arr3[0, 2]
first = arr3[1, 0]
second
np.concatenate((first, second))
array([ 7, 8, 9, 10, 11, 12])
Some homework
Research:
Adding/Removing Elements - resize()
- append()
- insert()
- delete()
Changing array shape - ravel()
- reshape()
# stacking
# np.vstack((a,b))
# np.hstack((a,b))
# np.column_stack((a,b))
# np.c_[a, b]
# splitting arrays
# np.hsplit()
# np.vsplit()